package com.v1.video.db.service;

import java.util.List;

import com.v1.video.AppContext;
import com.v1.video.db.dao.NewRecordTB;
import com.v1.video.db.dao.NewRecordTBDao;
import com.v1.video.dbcore.greenrobot.dao.query.QueryBuilder;

public class NewRecordDB {
	private static NewRecordDB mInstance;
	private NewRecordTBDao mNewRecordTBDao;

	private NewRecordDB() {
		mNewRecordTBDao = AppContext.getDaoSession().getNewRecordTBDao();
	}

	public static NewRecordDB getInstance() {
		if (mInstance == null) {
			mInstance = new NewRecordDB();
		}
		return mInstance;
	}
	
	public void insertOrReplace(NewRecordTB entity) {
		mNewRecordTBDao.insertOrReplace(entity);
	}
	
	public void insertOrReplace(String vid ) {
		NewRecordTB entity= new NewRecordTB();
		entity.setVid(vid);
		insertOrReplace(entity);
		deleteData();
	}
	
	public List<NewRecordTB> queryData() {
		QueryBuilder<NewRecordTB> query = mNewRecordTBDao.queryBuilder()
				.offset(0).limit(10);
		return query.build().list();
	}
	
	private void deleteData() {
		String sql = "delete from " + NewRecordTBDao.TABLENAME + " where "
				+ NewRecordTBDao.Properties.Vid.columnName + " not in(select "
				+ NewRecordTBDao.Properties.Vid.columnName + " from "
				+ NewRecordTBDao.TABLENAME + " order by "
				+ NewRecordTBDao.Properties.Id.columnName + " desc limit 0,10)";
		AppContext.getDaoMaster().getDatabase().execSQL(sql);
	}
}
